package api

import (
	"go-project/common"
	"go-project/common/base/resp"
	"go-project/internal/dao"
	"go-project/internal/model"
	"strconv"

	"github.com/gin-gonic/gin"
)

type userApi struct{}

var UserApi = new(userApi)

func (api *userApi) Route(hs *gin.RouterGroup) {
	g := hs.Group("/user")
	{
		g.GET("/:id", api.getById)
		g.POST("/:id", api.getById)
		g.GET("", api.page)
	}
}

//func (api *userApi) PublicRoute(hs *gin.RouterGroup) {
//	g := hs.Group("/user")
//	{
//		g.POST("/login", api.login)
//	}
//}

func (api *userApi) getById(c *gin.Context) {
	idStr := c.Param("id")
	id, _ := strconv.ParseUint(idStr, 10, 64)
	user, err := dao.UserDao.GetById(common.DB, id)
	resp.Data(c, user, err)
}

func (api *userApi) page(c *gin.Context) {
	query := new(model.UserDto)
	if err := c.BindQuery(query); err != nil {
		resp.BadRequest(c, err.Error())
		return
	}
	list, count, err := dao.UserDao.Page(common.DB, &query.Pager, &query.User)
	resp.Page(c, query.Pager, list, count, err)
}

//func (api *userApi) login(c *gin.Context) {
//	loginUser := new(model.UserDto)
//	err := c.BindJSON(loginUser)
//	if err != nil {
//		resp.BadRequest(c, err.Error())
//		return
//	}
//	token, err := biz.UserBiz.Login(loginUser)
//	resp.Data(c, token, err)
//}
